﻿using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Nito.AsyncEx;
using Serilog;

namespace Lawn.AppServer
{
    class Program
    {
       
        public static async Task Main(string[] args)
        {
            
           //  var remoteServer = new RemoteServer("127.0.0.1", 1234);
           //  remoteServer.TopicTypes.Add(new TopicType()
           //  {
           //              
           //      Topic = "top2",
           //      Next = true,
           //      NextTopic = "reply_top1"
           //  });
           //          
           //  var remoteServer2 = new RemoteServer("127.0.0.1", 1233);
           //  remoteServer2.TopicTypes.Add(new TopicType()
           //  {
           //              
           //      Topic = "top1",
           //      Next = true,
           //      NextTopic = "reply_top1"
           //  });
           //  var option = new OptionsRemoteServer();
           //  option.Server.Add(remoteServer);
           //  option.Server.Add(remoteServer2);
           // var json= JsonConvert.SerializeObject(option);
           //
           // Console.WriteLine(json);

           var config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
            
           Log.Logger = new LoggerConfiguration().Enrich.FromLogContext().ReadFrom.Configuration(config).CreateLogger();
           try
           {
               Log.Information("start");
               var host = CreateHostBuilder(args).Build();
               await host.RunAsync();
               
           }
           catch (Exception e)
           {
               Log.Error(e,e.Message);
           }
           
          
         
          
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostBuilderContext, serviceCollection)
                    => new Startup(hostBuilderContext.Configuration)
                        .ConfigureServices(serviceCollection)).UseSerilog(((context, configuration) =>
                {
                    configuration.ReadFrom.Configuration(context.Configuration);
                    configuration.Enrich.FromLogContext();
                } ));
        /*static async Task Main(string[] args)
        {
        
        
           var hostBuilder=  Host.CreateDefaultBuilder(args);
        
            
           
            IHost host = hostBuilder.ConfigureServices(services =>
                {
                     
        
                  //  services.Configure<OptionsPort>(x => configuration.GetSection("Server:Port"));
                  
                    
                  
                    var remoteServer = new RemoteServer("127.0.0.1", 1234);
                    remoteServer.TopicTypes.Add(new TopicType()
                    {
                        
                        Topic = "top1",
                        Next = true,
                        NextTopic = "reply_top1"
                    });
                    
                    var remoteServer2 = new RemoteServer("127.0.0.1", 1233);
                    remoteServer2.TopicTypes.Add(new TopicType()
                    {
                        
                        Topic = "top1233",
                        Next = true,
                        NextTopic = "reply_top1"
                    });
                    services.Configure<OptionsRemoteServer>(x =>
                    {
                        x.Server.Add(remoteServer);
                        x.Server.Add(remoteServer2);
                        
                    }); 
                    
                    services.Configure<HostOptions>(x =>
                    {
                        x.ShutdownTimeout = new TimeSpan(0, 0, 60);
                    }); 
                    services.AddHostedService<MqService>(); 
                    services.AddDefaultSerializeProvider();
                    services.AddMqService();
        
                }).UseSerilog(((context, configuration) =>
                {
                    configuration.ReadFrom.Configuration(context.Configuration);
                    configuration.Enrich.FromLogContext();
                } ))
                .Build();
        
           await host.RunAsync();
        
            
        
        }*/


    }
}